#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QShortcut>
#include <QDebug>
#include <QFileDialog>
#include <QTextEdit>
#include <QFontDialog>
#include <QColorDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //创建一个文本编辑器
    QTextEdit *textEdit = new QTextEdit(this);
    this->setCentralWidget(textEdit);

    //获取菜菜单栏
    QMenuBar *menuBar = this->menuBar();
    this->setMenuBar(menuBar);

    //创建菜单
    QMenu *menuFile = new QMenu("文件(&F)");
    QMenu *menuEdit = new QMenu("编辑(&E)");
    menuBar->addMenu(menuFile);
    menuBar->addMenu(menuEdit);

    //创建菜单项
    QAction *actionOpen = new QAction("打开文件");
    QAction *actionSave = new QAction("保存文件");
    QAction *actionFont = new QAction("文字(Ctrl + F)");
    QAction *actionColor = new QAction("颜色");
    menuFile->addAction(actionOpen);
    menuFile->addAction(actionSave);
    menuEdit->addAction(actionFont);
    menuEdit->addAction(actionColor);

    //创建QShortcut
    QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F),menuEdit);
    //绑定快捷键被按下信号槽
    connect(shortcut,&QShortcut::activated,actionFont,&QAction::trigger);

    //绑定菜单项信号槽
    connect(actionOpen,&QAction::triggered,this,&MainWindow::handleOpen);
    connect(actionSave,&QAction::triggered,this,&MainWindow::handleSave);
    connect(actionFont,&QAction::triggered,this,&MainWindow::handleFont);
    connect(actionColor,&QAction::triggered,this,&MainWindow::handleColor);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handleOpen()
{
    //创建一个打开文件对话框
    const QString &path = QFileDialog::getOpenFileName(this,"打开文件");

    //状态栏显示打开文件路径
    this->statusBar()->showMessage(path,3000);

    //进行文件操作
    QFile file(path);

    //打开文件
    bool ret = file.open(QFile::ReadOnly);
    if(!ret){
        //打开文件失败
        this->statusBar()->showMessage(path + "打开失败");
        return ;
    }

    //读取数据
    QString text = file.readAll();

    //关闭文件
    file.close();

    //显示到文本编辑器上
    QTextEdit *textEdit =(QTextEdit*)this->centralWidget();
    textEdit->setText(text);
}

void MainWindow::handleSave()
{
   //创建一个打开文件对话框
    const QString &path = QFileDialog::getSaveFileName(this,"保存文件");

    //状态栏显示打开文件路径
    this->statusBar()->showMessage(path,3000);

    //进行文件操作
    QFile file(path);

    //打开文件
    bool ret = file.open(QFile::WriteOnly);
    if(!ret){
        //打开文件失败
        this->statusBar()->showMessage(path + "打开失败");
        return ;
    }

    //写入数据
    QTextEdit *textEdit =(QTextEdit*)this->centralWidget();
    file.write(textEdit->toPlainText().toUtf8());

    //关闭文件
    file.close();

}

void MainWindow::handleFont()
{
    bool ok = false;
    QFont font = QFontDialog::getFont(&ok,this);

    QTextEdit *textEdit =(QTextEdit*)this->centralWidget();
    textEdit->setFont(font);

}

void MainWindow::handleColor()
{
    QColor color = QColorDialog::getColor(QColor(Qt::red),this);
    QTextEdit *textEdit =(QTextEdit*)this->centralWidget();
    char style[1024] = {0};
    snprintf(style,sizeof(style),"color: rgb(%d,%d,%d);",color.red(),color.green(),color.blue());
    textEdit->setStyleSheet(style);

}

